package com.hpin.fansea.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.bean.HouseRentBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.List;

/**
 * Created by Aaron on 15/9/16.
 */
public class HouseRentAdapter extends BaseAdapter {

    private Context context;
    private DisplayImageOptions options;

    private List<HouseRentBean.HouseRentInfo> houseRentInfos;

    public HouseRentAdapter(Context context, List<HouseRentBean.HouseRentInfo> houseRentInfos) {

        this.context = context;
        this.houseRentInfos = houseRentInfos;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.icon_loading_default_list)
                .showImageForEmptyUri(R.mipmap.icon_fail_default_list)
                .showImageOnFail(R.mipmap.icon_fail_default_list)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    public void setHouseRentInfos(List<HouseRentBean.HouseRentInfo> houseRentInfos) {
        this.houseRentInfos = houseRentInfos;
    }

    @Override
    public int getCount() {
        return houseRentInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder=null;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_house_rent, null);
            viewHolder = new ViewHolder();

            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_rent_price = (TextView) convertView.findViewById(R.id.tv_rent_price);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.iv_icon_pre = (ImageView) convertView.findViewById(R.id.iv_icon_pre);

            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        ImageLoader.getInstance().displayImage(houseRentInfos.get(position).pic, viewHolder.iv_icon_pre, options);

        viewHolder.tv_title.setText(houseRentInfos.get(position).title);
        viewHolder.tv_rent_price.setText(houseRentInfos.get(position).price);
        viewHolder.tv_time.setText(houseRentInfos.get(position).publishTime);

        return convertView;
    }

    private class ViewHolder {

        public TextView tv_title;
        public TextView tv_rent_price;
        public TextView tv_time;
        public ImageView iv_icon_pre;

    }


}
